define([ "modules/komunikat" ], function(komunikat) {
	return {
		createGrid : function(config) {
			require([ "dojox/grid/DataGrid", "dojo/_base/xhr", "dojo/domReady!", "dojo/data/ItemFileWriteStore", "modules/ep" ], function(DataGrid, xhr, dom, ifws, ep) {
				
				var listStore = null;
				var columnModelWyslane = [{
					name : "Data",
					field : "data",
					width : "10%"
				},{
					name : "Temat",
					field : "temat",
					width : "20%"
				}, {
					name : "Treść",
					field : "tresc",
					width : "70%",
					formatter : function (value, rowIndex){
						return "...";
					}					
				}];
				
				var columnModelOdebrane = [{
					name : "Autor",
					field : "autorNazwa",
					width : "15%"
				},{
					name : "Data",
					field : "data",
					width : "10%"
				},{
					name : "Temat",
					field : "temat",
					width : "20%"
				}, {
					name : "Treść",
					field : "tresc",
					width : "55%",
					formatter : function (value, rowIndex){
						return "...";
					}
				}];
				
				var grid = new DataGrid({
					id: config.gridId,
					height: '500px',
					escapeHTMLInData: false,
					query : {
						id : "*"
					},
					queryOptions : {},
					structure : config.wyslane === 'true'? columnModelWyslane : columnModelOdebrane,
					onRowClick : function(e) {
						var row = this.getItem(e.rowIndex);
						var text = row.tresc;
						ep.showMsgCustom(text, "Treść wiadomości");
					},
					reloadStore: function() {
						xhr.get({
							url : config.url,
							handleAs : "json"
						}).then(function(data2) {
							   listStore = new dojo.data.ItemFileWriteStore({
							        data: {
							            items: data2
							        }
							    });
							   grid.setStore(listStore);
						});
					},
					setUrl : function(url) {
						config.url = url;
					}
				}, config.gridDiv);

				grid.reloadStore();
				grid.startup();
			});
		},
		createNowyKomunikatWindow : function(config) {
			require(["dojo/ready",
			         "dijit/Editor",
			         "dijit/_editor/plugins/AlwaysShowToolbar",
			         "dijit/_editor/plugins/FontChoice",
			         "dojo/dom"], function(ready, Editor, AlwaysShowToolbar, FontChoice, dom){
			        new Editor({
			            height: '300px',
			            name: 'editMessage',
			            plugins: ["undo", "redo", "|",
			                      "cut","copy","paste","|",
			                      "bold","italic", "underline", "strikethrough", "|",
			                      "indent", "outdent", "justifyLeft", "justifyCenter", "justifyRight" ,"|",
			                      "fontName", "fontSize"]
			        }, dom.byId(config.div));
			});
		},
		createGridOdbiorcy : function(config) {
			require([ "dojox/grid/EnhancedGrid",
			          "dojo/_base/xhr",
			          "dojo/domReady!",
			          "dojo/data/ItemFileWriteStore",
			          "dojox/grid/enhanced/plugins/IndirectSelection"], function(EnhancedGrid, xhr, domReady, ifws, is) {
				
				var listStore = null;
				var columnModel = [{
					name : "Rodzaj odbiorcy",
					field : "rodzaj",
					width : "20%"
				},{
					name : "Imie",
					field : "imie",
					width : "40%"
				}, {
					name : "Nazwisko",
					field : "nazwisko",
					width : "40%"
				}];
				
				var grid = new EnhancedGrid({
					id: config.gridId,
					title: 'Odbiorcy',
			        rowSelector: '20px',
					query : {
						id : "*"
					},
					queryOptions : {},
					structure : columnModel,
					plugins: {indirectSelection: {headerSelector:true, width:"40px", styles:"text-align: center;"}},
					reloadStore: function() {
						xhr.get({
							url : config.url,
							handleAs : "json"
						}).then(function(data2) {
							   listStore = new dojo.data.ItemFileWriteStore({
							        data: {
							            items: data2
							        }
							    });
							   grid.setStore(listStore);
						});
					},
					setUrl : function(url) {
						config.url = url;
					}
				}, config.gridDiv);

				grid.reloadStore();
				grid.startup();
			});
		}
	}
});